package xio.ccf_201709;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 201709-3 Json 解析 90分
 * Created by Xio on 2017/10/24.
 */
public class JsonParser {

    static Map<String,String> map = new HashMap<>();
    static int index = 0;  //全局 表示当前字符位置
    static char[] src; //输入的内容存储到字符数组里
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        sc.nextLine();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(sc.nextLine());
        }
        //清空全部空格字符
        src = sb.toString().trim().replaceAll("\\s+","").toCharArray();
        index++;
        parseObject("");

        //输出结果
        for (int i = 0; i < m; i++) {
            String value = map.get(sc.nextLine());
            if (value == null){
                System.out.println("NOTEXIST");
            }else if (value.isEmpty()) {
                System.out.println("OBJECT");

            }else {
                    System.out.println("STRING "+value);
                }
            }
    }

    //解析jsonObject （以{ 开头）
    private static void parseObject(String preKey){
        while(index < src.length){
            if (src[index] == '"') {
                index++;
                String str = parseString();

                String key = preKey.isEmpty() ? str : preKey + "." + str;
                index++;
                char tmp = src[index];
                if (tmp == '"') {
                    index++;
                    map.put(key, parseString());
                } else if (tmp == '{') {
                    map.put(key, "");
                    index++;
                    parseObject(key);
                }

                if (src[index] == '}') {
                    index++;
                    break;
                } else if (src[index] == ',') index++;
            }
        }
    }

    //解析字符串（以” 开头）得到json里的key 或 value
    private static String parseString(){

       StringBuilder sb = new StringBuilder();
        while (index < src.length){
            if (src[index] == '"'){
                index++;
                return sb.toString();
            }

            if (src[index] == '\\'){
                index++;   // 忽略 \ 录入下一个字符
                sb.append(src[index]);

            }else {
                sb.append(src[index]);
            }
            index++;
        }
        return "";
    }

}
